package com.gm.englishdemo3.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class Base64Util {
    /**
     * 更具传入进来的base64字符串转换为字节流图片
     */
    public static Bitmap getBitMap(String base64Str){
        Bitmap bit=null;
        if(base64Str==null){
            return null;
        }
        System.out.println(base64Str);
        if(base64Str.substring(0,4).equals("http")){
            bit=getURLimage(base64Str);
        }
        byte[] decode= Base64.decode(base64Str,Base64.DEFAULT);

        bit= BitmapFactory.decodeByteArray(decode,0,decode.length);
        return bit;
    }
    public static Bitmap getURLimage(String url) {
        Bitmap bmp = null;
        try {
            URL myurl = new URL(url);
            // 获得连接
            HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
            conn.setConnectTimeout(6000);//设置超时
            conn.setDoInput(true);
            conn.setUseCaches(false);//不缓存
            conn.connect();
            InputStream is = conn.getInputStream();//获得图片的数据流
            bmp = BitmapFactory.decodeStream(is);
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bmp;
    }


}
